package com.chunayev.numerology.main;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;


import com.chunayev.numerology.gui.frame.NumerologyFrame;
import com.chunayev.numerology.gui.util.ComponentDisplayManager;
import com.chunayev.numerology.util.db.DBInitializer;
import com.sleepycat.je.DatabaseException;

/**
 * @author Sergey Chunayev
 */
public class Main {
    
    /**
     * @param args
     */
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            
            @Override
            public void run() {
                try {
                    final JFrame frame = new NumerologyFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    ComponentDisplayManager.centralize(frame);
                    frame.addWindowListener(new WindowAdapter() {
                        
                        /*
                         * (non-Javadoc)
                         * @see
                         * java.awt.event.WindowAdapter#windowClosing(java.awt
                         * .event.WindowEvent)
                         */
                        @Override
                        public void windowClosing(final WindowEvent e) {
                            super.windowClosing(e);
                            try {
                                DBInitializer.close();
                            } catch (final DatabaseException ex) {
                                throw new RuntimeException(ex);
                            }
                        }
                    });
                    frame.setVisible(true);
                    
                } catch (final Exception e) {
                    e.printStackTrace();
                }
            }
            
        });
    }
    
}
